import axios from '../../config/http.js'
import * as url from '../../api/api.js'
const state = {
    user: {
        name: localStorage.getItem('userName') || undefined,
        token: localStorage.getItem('token') || undefined,
        type: localStorage.getItem('type') || undefined
    }
};
const actions = {
    userLogin({ commit }, obj) {
        let { user, pwd } = obj
        return axios({
            url: url.LOGIN,
            data: { user, pwd },
            method: 'post'
        }).then(res => {
            console.log(res.data)

            if (res.data.code === 200) {
                commit('addUser', res) //代表第一步做完了
                localStorage.setItem('userName', res.data.user)
                localStorage.setItem('type', res.data.type)
            }
            return res.data //第二步开始  以及第二步需要的东西
        }).catch(err=>{
          console.log(err)
        })
    },
    outLogin({ commit }) {
        localStorage.removeItem('userName')
        localStorage.removeItem('type')
        localStorage.removeItem('token')
        commit('outLogin')
    }
};
const mutations = {
    addUser(state, obj) {
        state.user.name = obj.data.user
        state.user.type = obj.data.type
    },
    outLogin(state) {
        state.user.name = undefined
        state.user.type = undefined
        state.user.token = undefined
    }
}
const getters = {

};
export default {
    state,
    actions,
    mutations,
    getters
}